package com.example.androidkotlinmvvm

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModelProvider
import com.example.androidkotlinmvvm.mvvm.User2ViewModel
import com.example.androidkotlinmvvm.mvvm.UserViewModel

class DataActivity : ComponentActivity() {
    private var userViewModel:UserViewModel?=null
    private var userViewModel2: User2ViewModel?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        userViewModel=UserViewModel()
        setContent {
            Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                Column(modifier = Modifier.padding(innerPadding)) {
                    dataLayout()
                    buttonLayout()
                }
            }
        }
    }

    @Composable
    fun buttonLayout(){
//        userViewModel2 = User2ViewModel()
//        val user by userViewModel2!!.user!!.collectAsState()
        Button(onClick = {
            userViewModel?.loadUser()
        }) {
            Text(text = "点击")
        }
    }
    
    @Composable
    fun dataLayout(){
        val user by userViewModel!!.user!!.collectAsState()
        Text(text = "${user!!.id}")
        Text(text = "${user!!.name}")
    }
}